package com.we823.project.repository.service.repo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.we823.project.model.repo.CategoryModel;
import com.we823.project.model.repo.MaterialModel;
import com.we823.project.repository.dao.repo.MaterialRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("materialService")
public class MaterialService extends AbstractBaseService<MaterialModel> {

	private MaterialRepository materialRepository;
	
	@Autowired
	public MaterialService(MaterialRepository materialRepository) {
		super(materialRepository);
		this.materialRepository = materialRepository;
	}

	public Page<MaterialModel> findByCategory(CategoryModel categoryModel, PageRequest pageRequest) {
		if(pageRequest==null){
			return new PageImpl<>(materialRepository.findByCategory(categoryModel));
		}
		return materialRepository.findByCategory(categoryModel,pageRequest);
	}

	public Page<MaterialModel> findByFullnameContaining(String fullname,Pageable pageable) {
		return materialRepository.findByFullnameContaining(fullname,pageable);
	}

	public MaterialModel findByFullnameAndColor(String modelString, String color) {
		return materialRepository.findByFullnameOrSpecificationAndColor(modelString,modelString,color);
	}

	public MaterialModel findByModelAndColor(String model, String color) {
		return materialRepository.findByModelAndColor(model,color);
	}

	public MaterialModel findByCode(String code) {
		return materialRepository.findByCode(code);
	}

	public List<MaterialModel> findByCodeIn(List<String> materialCodeList) {
		return materialRepository.findByCodeIn(materialCodeList);
	}

	public Page<MaterialModel> findByCodeContaining(String queryString, PageRequest pageRequest) {
		return materialRepository.findByCodeContaining(queryString,pageRequest);
	}

	public Map<String, MaterialModel> getMapByCategory(
			CategoryModel categoryModel) {
		Map<String, MaterialModel> map = new HashMap<String, MaterialModel>();
		List<MaterialModel> materialModels = null;
		if(categoryModel!=null) {
			materialModels = materialRepository.findByCategory(categoryModel);
		}else {
			materialModels = materialRepository.findAll();
		}
		if(materialModels!=null && materialModels.size()>0) {
			for (MaterialModel materialModel : materialModels) {
				map.put(materialModel.getCode(), materialModel);
			}
		}
		return map;
	}

	public List<MaterialModel> findByCategoryIn(
			List<CategoryModel> categoryModels) {
		return materialRepository.findByCategoryIn(categoryModels);
	}

	/**
	 * code_materialModel
	 * @return
	 */
	public Map<String, MaterialModel> map() {
		List<MaterialModel> materialModels = findAll();
		if(materialModels!=null) {
			Map<String, MaterialModel> map = new HashMap<>();
			for (MaterialModel materialModel : materialModels) {
				map.put(materialModel.getCode(), materialModel);
			}
			return map;
		}
		return null;
	}

	public Map<String, MaterialModel> mapByMaterialCodeList(
			List<String> materialCodeList) {
		List<MaterialModel> materialModels = this.findByCodeIn(materialCodeList);
		if (materialModels!=null && materialModels.size()>0) {
			Map<String, MaterialModel> map = Maps.newHashMap();
			for (MaterialModel materialModel : materialModels) {
				map.put(materialModel.getCode(), materialModel);
			}
			return map;
		}
		return null;
	}

	public Map<String, MaterialModel> mapByBom() {
		List<MaterialModel> materialModels = findAll();
		if (materialModels!=null && materialModels.size()>0) {
			Map<String, MaterialModel> map = Maps.newHashMap();
			for (MaterialModel materialModel : materialModels) {
				
				String fullname = materialModel.getFullname();
				String color = materialModel.getColor();
				String key = fullname+"_"+color;
				String key2 = StringUtils.replace(fullname, " ", "")+"_"+color;
				if(!key.equals(key2)) {
				   map.put(key2, materialModel);
				}
				map.put(key, materialModel);
			}
			
			return map;
		}
		return null;
	}

	public void save(List<MaterialModel> materialModels) {
		materialRepository.save(materialModels);
	}

}
